package gptma.fo.controler;

import gptma.bo.model.employee.Employee;
import gptma.fo.aop.Current;
import java.text.MessageFormat;
import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import javax.inject.Inject;

public abstract class WebPage {

	@Inject
	protected Application application;

	@Inject
	protected Session session;

	@Inject
	@Current
	protected Employee user;

	protected void addInfoMessageFromKey(String messageKey) {
		FacesMessage msg = new FacesMessage(getLocalisedMessage(messageKey),
				null);
		msg.setSeverity(FacesMessage.SEVERITY_INFO);
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	protected void addInfoMessageFromKey(String messageKey, Object... params) {
		FacesMessage msg = new FacesMessage(getLocalisedMessage(messageKey,
				params), null);
		msg.setSeverity(FacesMessage.SEVERITY_INFO);
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	protected void addErrorMessageFromKey(String messageKey) {
		FacesMessage msg = new FacesMessage(getLocalisedMessage(messageKey),
				null);
		msg.setSeverity(FacesMessage.SEVERITY_ERROR);
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	protected void addErrorMessageFromKey(String messageKey, Object... params) {
		FacesMessage msg = new FacesMessage(getLocalisedMessage(messageKey,
				params), null);
		msg.setSeverity(FacesMessage.SEVERITY_ERROR);
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	protected String getLocalisedMessage(String key) {
		ResourceBundle bdl = ResourceBundle.getBundle(Application.MSG_BDL);
		return bdl.getString(key);
	}

	protected String getLocalisedMessage(String key, Object... params) {
		return MessageFormat.format(getLocalisedMessage(key), params);
	}

}
